<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="onConfirm"
    @cancel="$emit('close')"
  />
</template>

<script>
import { updateUserInfo } from '@/api/user.js'
import dayJs from 'dayjs'
import { Toast } from 'vant'
export default {
  props: {
    value: {
      type: String,
      required: true

    }
  },
  data () {
    return {
      currentDate: null,
      minDate: new Date('1900-01-01'),
      maxDate: new Date('2021-12-07')
    }
  },

  created () {
    this.currentDate = new Date(this.value)
  },

  methods: {
    async  onConfirm (val) {
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0

      })
      const formateTime = dayJs(val).format('YYYY-MM-DD')
      try {
        await updateUserInfo({
          birthday: formateTime
        })
        Toast.success('修改成功')
        this.$emit('close')
        this.$emit('input', formateTime)
      } catch (err) {
        Toast.fail('修改失败')
      }
    }
  }
}
</script>

<style scoped></style>
